package com.mask.juc.v1.c_007;

import java.util.ArrayList;

/**
 * volatile不能保证多个线程共同修改running变量时所带来的不一致问题，因此volatile不能替代synchronized
 * @author hx
 * @date 2021/10/12 10:01 上午
 */

public class T {
    volatile int count = 0;
    void m(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public static void main(String[] args) {
        T t = new T();
        ArrayList<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            threads.add(new Thread(t::m, "thread-"+i));
        }
        threads.forEach((o) -> o.start());
        threads.forEach((o) ->{
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(t.count);
    }
}
